package com.biglead.admin.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.admin.mapper.UmsResourceCategoryMapper;
import com.biglead.admin.pojo.UmsResourceCategory;
import com.biglead.admin.service.UmsResourceCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 后台用户管理Service实现类
 * Created by macro on 2018/4/26.
 */
@Service("umsResourceCategoryService")
public class UmsResourceCategoryServiceImpl extends ServiceImpl<UmsResourceCategoryMapper, UmsResourceCategory> implements UmsResourceCategoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsResourceCategoryServiceImpl.class);


    /**
     * 添加资源
     *
     * @param umsResource
     */
    @Override
    public boolean create(UmsResourceCategory umsResource) {
        umsResource.setCreateTime(new Date());
        return this.save(umsResource);
    }

    /**
     * 修改资源
     *
     * @param id
     * @param umsResource
     */
    @Override
    public boolean update(Long id, UmsResourceCategory umsResource) {
        umsResource.setId(id);

        return this.updateById(umsResource);
    }

    /**
     * 获取资源详情
     *
     * @param id
     */
    @Override
    public UmsResourceCategory getItem(Long id) {
        return this.getById(id);
    }

    /**
     * 删除资源
     *
     * @param id
     */
    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    /**
     * 分页查询资源
     *
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @param pageSize
     * @param pageNum
     */
    @Override
    public Page<UmsResourceCategory> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        Page<UmsResourceCategory> page = Page.of(pageNum, pageSize);
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<UmsResourceCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(nameKeyword)) {
            //查询条件 name 模糊
            queryWrapper.like(UmsResourceCategory::getName, nameKeyword);
        }
        //发起查询
        this.page(page, queryWrapper);
        return page;
    }

    @Override
    public List<UmsResourceCategory> listAll() {
        return this.list(new LambdaQueryWrapper<UmsResourceCategory>());
    }

}
